﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Maticsoft.Web.Admin.Shopping
{
    public partial class ProductList : PageBase
    {
        string key = string.Empty;
        int categoryId, isHid;
        BLL.Products proBLL = new BLL.Products();
        protected void Page_Load(object sender, EventArgs e)
        {
            Check("shopping", "read");

            key = Request.QueryString["key"];
            int.TryParse(Request.QueryString["categoryId"], out categoryId);
            if (!int.TryParse(Request.QueryString["isHid"], out isHid))
            {
                isHid = -1;
            }

            if (!IsPostBack)
            {
                LoadList();
                BindData();
            }
        }

        protected void BindData()
        {
            txtKey.Value = key;
            ddlCategory.SelectedValue = categoryId.ToString();
            ddlHidden.SelectedValue = isHid.ToString();

            string strWhere = "IsActive=1";
            if (categoryId!=0)
            {
                strWhere += " and CategoryId=" + categoryId;
            }
            if (!string.IsNullOrEmpty(key))
            {
                strWhere += " and (ProductName like '%" + key + "%' or ProductNo like '%" + key + "%')";
            }
            if (isHid != -1)
            {
                strWhere += " and isnull(isHid,0)=" + isHid;
            }
            //strWhere += " order by BookId desc";
            int recordCount = proBLL.GetRecordCount(strWhere);
            AspNetPager1.RecordCount = recordCount;
            DataSet ds = proBLL.GetListByPageView(strWhere, "ProductId desc", AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex);
            Repeater1.DataSource = ds.Tables[0].DefaultView;
            Repeater1.DataBind();
        }

        /// <summary>
        /// 跳转页面
        /// </summary>
        private void Redirect()
        {
            Response.Redirect("ProductList.aspx?categoryId=" + ddlCategory.SelectedValue + "&isHid=" + ddlHidden.SelectedValue + "&key=" + txtKey.Value.Trim());
        }

        protected void ImageButton1_Click(object sender, EventArgs e)
        {
            Redirect();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string ids = txtIds.Value;
            if (!string.IsNullOrWhiteSpace(ids))
            {
                proBLL.DelLogic(ids);
            }

            Redirect();
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            BindData();
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            UpDown(0);
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            UpDown(1);
        }
        /// <summary>
        /// 上下架产品
        /// </summary>
        /// <param name="status">1上架 0下架</param>
        void UpDown(int status)
        {
            string ids = txtIds.Value;
            if (!string.IsNullOrWhiteSpace(ids))
            {
                proBLL.HiddenShow(ids, status);
            }

            Redirect();
        }

        BLL.Categorys cateBll = new BLL.Categorys();
        /// <summary>
        /// 加载整个分类表
        /// </summary>
        private void LoadList()
        {
            int MTcount = 0;//执行遍历的次数
            //开始迭代加载
            MakeTr("0", MTcount);

        }
        /// <summary>
        /// 装配每一行
        /// </summary>
        /// <param name="id">上级ID</param>
        /// <param name="count">迭代次数</param>
        private void MakeTr(string id, int count)
        {
            DataView dv = new DataView(cateBll.GetList("parentID=" + id + " order by CategoryId asc").Tables[0]);
            foreach (DataRowView drv in dv)
            {
                //形成下拉列表
                ListItem li = new ListItem(MakeFelgefu(count) + drv["CategoryName"].ToString(), drv["CategoryId"].ToString());
                ddlCategory.Items.Add(li);

                //再次遍历
                MakeTr(drv["CategoryId"].ToString(), count + 1);
            }

        }
        /// <summary>
        /// Make分隔符
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        private string MakeFelgefu(int count)
        {
            string Returnwords = string.Empty;
            if (count == 0)
            {
                Returnwords = "";
            }

            else
            {
                Returnwords = ("┠").PadLeft(count, '　');
            }
            return Returnwords;
        }
    }
}